package com.tanhua.server.service;

import com.tanhua.dubbo.api.UserInfoApi;
import com.tanhua.dubbo.api.UserLikeApi;
import com.tanhua.dubbo.api.VisitorsApi;
import com.tanhua.model.domain.User;
import com.tanhua.model.domain.UserInfo;
import com.tanhua.model.mongo.UserLike;
import com.tanhua.model.mongo.Visitors;
import com.tanhua.model.vo.Counts;
import com.tanhua.model.vo.PageResult;
import com.tanhua.server.interceptor.UserHolder;
import org.apache.dubbo.config.annotation.DubboReference;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.List;

/**
 * @description:test
 * @create by : 芒果的布丁鸭
 * @Date: 2021/12/10 10:37
 */
@Service
public class CountsService {

    @DubboReference
    private UserLikeApi userLikeApi;

    @DubboReference
    private UserInfoApi userInfoApi;

    @DubboReference
    private VisitorsApi visitorsApi;

    /**
     * 统计互相喜欢、喜欢、粉丝的数量
     * @return
     */
    public Counts Counts() {
        User user = UserHolder.get();
        Counts counts = new Counts();
        counts.setLoveCount(userLikeApi.findLoverCount(user.getId()));
        Long eachLoveCount = userLikeApi.findEachLoveCount(user.getId());
        counts.setEachLoveCount(eachLoveCount);
        counts.setFanCount(userLikeApi.findFanCount(user.getId()));
        return counts;
    }


    public PageResult PageLoverCount(Integer page, Integer pagesize, Integer type,String nickname) {
        //当前登录用户
        User user = UserHolder.get();

        PageResult pageResult = new PageResult();
        pageResult.setPagesize(pagesize);
        pageResult.setPage(page);
        pageResult.setCounts(0l);
        pageResult.setPages(0l);

        List<Long> userIds = new ArrayList();
        if (type == 2){
            List<UserLike> loverUsers = userLikeApi.findLoverUsers(user.getId(), page, pagesize);
            for (UserLike loverUser : loverUsers) {
                userIds.add(loverUser.getUserId());
            }
        }else if (type == 3){
            List<UserLike> fanUsers = userLikeApi.findFanUsers(user.getId(), page, pagesize);
            for (UserLike fanUser : fanUsers) {
                userIds.add(fanUser.getUserId());
            }
        }else if (type == 1){
            List<UserLike> eachLikeUsers = userLikeApi.findEachLikeUsers(user.getId(), page, pagesize);
            for (UserLike eachLikeUser : eachLikeUsers) {
                userIds.add(eachLikeUser.getUserId());
            }
        }else if (type == 4){
            List<Visitors> visitors = visitorsApi.visitorList(user.getId(),page,pagesize);
            for (Visitors visitor : visitors) {
                userIds.add(visitor.getVisitorUserId());
            }
        }
//        QueryWrapper queryWrapper = new QueryWrapper();
//        queryWrapper.in("userId",userIds );
//        List<UserInfo> list = this.userInfoApi.findLikelist(queryWrapper);

        List<UserInfo> userInfos = new ArrayList<>();
        for (Long userId : userIds) {
            UserInfo userInfo = userInfoApi.findById(userId);
            userInfos.add(userInfo);
        }


        pageResult.setItems(userInfos);

        return pageResult;
}

    public void likeid(Long userId) {
        User user = UserHolder.get();
        userLikeApi.likeid(user.getId(), userId);
    }

    public void fansid(Long userId) {
        User user = UserHolder.get();
        userLikeApi.fansid(user.getId(), userId);
    }
}
